﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.Threading;

/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: 
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 4/3/2015 10:12:28 PM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevGrammar.TestBasic
{
	[TestClass]
	public class TestProcess
	{
		[TestMethod]
		public void test_when_kill_process()
		{
			Process p = Process.Start("notepad.exe");
			Thread.Sleep(4000);
			p.Kill();
		}

		[TestMethod]
		public void test_get_all_process_array()
		{
			Process[] processList = Process.GetProcesses();
			Console.WriteLine(processList.Length);
			// All the process name in task manager is the exe name
			for (int i = 0; i < processList.Length; i++)
				Console.WriteLine(processList[i].ProcessName);
			
		}

		[TestMethod]
		public void test_process_status()
		{
			Process p = Process.Start("notepad.exe");
			
			Console.WriteLine(p.HasExited);
			p.WaitForExit();
			Console.WriteLine(p.HasExited);
		}

		[TestMethod]
		public void test_cmd_output()
		{
			ProcessStartInfo psi = new ProcessStartInfo("cmd", "/c dir");
			// The two setting below is necessary
			psi.RedirectStandardOutput = true;
			psi.UseShellExecute = false;

			Process p = Process.Start(psi);
			String executeResult = p.StandardOutput.ReadToEnd();
			Console.WriteLine(executeResult);
		}

		[TestMethod]
		public void test_start_exe_hide_window()
		{
			// TODO:It should hide window but nothing happened ! ! ! !
			Process myProcess = new Process();

			try
			{
				myProcess.StartInfo.UseShellExecute = false;
				// You can start any process, HelloWorld is a do-nothing example.
				myProcess.StartInfo.FileName = "C:\\Test_MFCApp.exe";
				myProcess.StartInfo.CreateNoWindow = true;
				myProcess.Start();
				// This code assumes the process you are starting will terminate itself.  
				// Given that is is started without a window so you cannot terminate it  
				// on the desktop, it must terminate itself or you can do it programmatically 
				// from this application using the Kill method.
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}

		}
	}
}
